<template>
  <div class="container">
    <div class="header">
      <div class="left">
        <a href="/">
          <img class="logo" src="@/assets/logo2.jpg" alt="logo">
          <span class="mall-title">商城系统</span>
        </a>
      </div>
    </div>

    <el-form :model="form" ref="form" :rules="rules" label-width="80px" class="login-form">
      <h2 class="title">商城系统登录页面</h2>
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" clearable show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="toLogin('form')" type="primary" class="login-button">登录</el-button>
        <el-button type="danger" @click="toReg" class="register-button">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', onTrigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    toLogin(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$message({
            message: "登录成功",
            showClose: true,
            type: "success",
            duration: 3000
          })
          this.$store.dispatch('Login', this.form)
          this.$router.push("/")
        } else {
          return false
        }
      })
    },
    toReg() {
      this.$router.push("/reg")
    }
  }
}
</script>

<style scoped>
.container {
  /* background-image: linear-gradient(to top, #fbc2eb 0%, #a6c1ee 100%); */
  /* background-image: linear-gradient(120deg, #d4fc79 0%, #96e6a1 100%); */
  background-image: linear-gradient(to top, #a8edea 0%, #fed6e3 100%);
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.header {
  line-height: 50px;
  background-color: #ffffff;
  display: flex;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  width: 100%;
}

.left {
  display: flex;
  align-items: center;
}

.logo {
  vertical-align: middle;
  width: 30px;
  height: 30px;
  margin: 0 10px 0 40px;
}

.mall-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.title {
  text-align: center;
  color: #444;
  margin-bottom: 20px;
}

.login-form {
  width: 450px;
  margin: auto;
  background-color: #ffffff;
  padding: 30px;
  border-radius: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.el-form-item label {
  color: #333;
}

.el-input__inner {
  border-radius: 20px;
}

.el-button {
  border-radius: 20px;
  padding: 10px 20px;
}

.login-button {
  background-color: #409eff;
  border-color: #409eff;
  color: white;
  font-weight: bold;
}

.login-button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.register-button {
  background-color: #f56c6c;
  border-color: #f56c6c;
  color: white;
  font-weight: bold;
}

.register-button:hover {
  background-color: #ff7878;
  border-color: #ff7878;
}
</style>
